﻿using System;
using System.Linq;
using System.Windows.Forms;
using HighDiveEngine.Aggregates;
using HighDiveEngine.Model;
using HighDiveEngine.Resources;

namespace HighDiveAdmin.GUI.Components
{
    public partial class EditContestDiverList : UserControl
    {
        public event DelegateVoidInt EventAddDiver;
        public event DelegateVoidString EventLoadDiver;
        public event DelegateDiverEventArgs EventRemoveDiver;
        public event DelegateDiveEventArgs EventRemoveDive;
 
        public EditContestDiverList()
        {
            InitializeComponent();
        }
        

        public void Show(DiveList diveList)
        {
            FormMain.PopulateTreeFromDiveList(treeViewDivers, diveList, true);
            Visible = true;
            
        }

        private void buttonAddDiver_Click(object sender, EventArgs e)
        {
            if (EventAddDiver != null)
            {
                EventAddDiver(treeViewDivers.Nodes.OfType<DiverTreeNode>().Count() + 1);
                Visible = false;
            }
        }

        private void buttonEditDiver_Click(object sender, EventArgs e)
        {
            if (treeViewDivers.SelectedNode.GetType() != typeof (DiverTreeNode)) return;
            if (EventLoadDiver != null)
                EventLoadDiver(((DiverTreeNode) treeViewDivers.SelectedNode).Diver.IdNumber);
        }

        private void treeViewDivers_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            buttonEditDiver_Click(null, null);
        }

        private void buttonRemoveDiver_Click(object sender, EventArgs e)
        {
            if (treeViewDivers.SelectedNode != null)
            {
                if (treeViewDivers.SelectedNode.GetType() == typeof(DiverTreeNode))
                {
                    if (EventRemoveDiver != null)
                    {
                        var diverArgs = new DiverEventArgs()
                        {
                            DbId = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.DbId,
                            Address = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.Address,
                            Club = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.Club,
                            Email = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.Email,
                            FirstName = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.FirstName,
                            Gender = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.Gender,
                            IdNumber = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.IdNumber,
                            LastName = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.LastName,
                            StartNumber = ((DiverTreeNode)treeViewDivers.SelectedNode).Diver.StartNumber
                        };
                        EventRemoveDiver(diverArgs);
                    }
                }
                else if (treeViewDivers.SelectedNode.GetType() == typeof(DiveTreeNode))
                {
                    if (EventRemoveDive != null)
                    {
                        var diveArgs = new DiveEventArgs()
                        {
                            DbId = ((DiveTreeNode)treeViewDivers.SelectedNode).Dive.DbId,
                            Diver = ((DiveTreeNode)treeViewDivers.SelectedNode).Dive.Diver,
                            Difficulty = ((DiveTreeNode)treeViewDivers.SelectedNode).Dive.Difficulty,
                            DiveCode = ((DiveTreeNode)treeViewDivers.SelectedNode).Dive.DiveCode,
                            DiveHeight = ((DiveTreeNode)treeViewDivers.SelectedNode).Dive.DiveHeight,
                            DiveNumber = ((DiveTreeNode)treeViewDivers.SelectedNode).Dive.DiveNumber
                        };
                        EventRemoveDive(diveArgs);
                    }
                }
            }
        }


        #region TabStop
        private void EditContestDiverList_TabStopChanged(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
                control.TabStop = TabStop;
            if(TabStop)
                buttonAddDiver.Focus();
        }

        private void EditContestDiverList_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
                buttonAddDiver.Focus();
        }
        #endregion

        private void treeViewDivers_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonEditDiver_Click(null, null);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                buttonRemoveDiver_Click(null, null);
            }
        }
    }
}
